// pages/person/person.js
let myRequest = require('../../common/script/myRequest.js');
let api = require('../../common/script/api.js');
let login = require('../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isnoshowCard: null,
    headOfStatus: false,
    recommendStatus: false,
    memberData: null,
    allowance: true,
    ticket_bill: false
  },

  //是否显示群津贴
  onAllowance() {
    const that = this;
    let allowance = that.data.allowance;
    that.setData({
      allowance: !allowance
    })
  },
  getData() {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.membercardShow, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        that.setData({
          memberData: res.data.result.member_data,
          isnoshowCard: true,
        })
        wx.setStorageSync('memberData', res.data.result.member_data)
      } else if (res.data.status == -2) {
        that.setData({
          isnoshowCard: false,
          memberData: null
        })
        wx.setStorageSync('memberData', null)
      } else if (res.data.status == -101 || res.data.status == -100) {
        login.loginByWeixin().then(res => {
          app.globalData.token = res.data.result.token
          that.getData()
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  //查询状态
  geStatus() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    myRequest.EasyHttp.post(api.reqUrls.checkshow, data).then(res => {
      that.setData({
        headOfStatus: res.data.result.check.teamer == 0 ? false : true,
        recommendStatus: res.data.result.check.recom == 0 ? false : true,
        ticket_bill: res.data.result.check.ticket_bill == 0 ? false : true,
      })
    })
  },
  //注册会员
  toAplicationCard() {
    const that = this;
    wx.navigateTo({
      url: `/page/details/pages/aplicationCard/aplicationCard?type=true`
    })
  },
  // 绑定我的会员
  toBindSwapCard() {
    const that = this;
    wx.navigateTo({
      url: `/package/pages/swapCard/swapCard?type=1`
    })
  },
  //充值记录
  toRechargeRecord() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/rechargeRecord/rechargeRecord`
    })
  },
  // 消费记录
  toSpending() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/spending/spending`
    })
  },
  //会员卡详情
  toCardDetail() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/cardDetail/cardDetail`
    })
  },
  // 申请团长
  toApplyLeader() {
    const that = this;
    wx.navigateTo({
      url: `/package/pages/applyLeader/applyLeader`
    })
  },
  //团返现
  toDeputyLeader() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/page/details/pages/deputyLeader/deputyLeader`
    })
  },
  // 换卡
  toSwapCard() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/swapCard/swapCard?type=2`
    })
  },
  // 推荐码
  toReferralCode() {
    const that = this;

    wx.navigateTo({
      url: `/page/details/pages/referralCode/referralCode`
    })
  },
  // 群友码
  toGroupFriend() {
    const that = this;
    wx.navigateTo({
      url: `/package/pages/groupFriend/groupFriend`
    })
  },
  // 我的订单
  toOrder() {
    wx.navigateTo({
      url: `/goods/pages/order/order`
    })
  },
  //发票
  toInvoice() {
    const that = this;
    wx.navigateTo({
      url: `/package/pages/invoice/invoice`
    })
  },
  // 跳转到优惠券页面
  toCoupon() {
    wx.setStorageSync('refreshCoupon', true)
    wx.switchTab({
      url: '/pages/coupon/coupon'
    })
  },
  //充值金额
  toTecharge() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/recharge/recharge`,
    })
  },
  // 付款
  toPayment() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/payment/payment`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      // that.getData()
      that.geStatus()
    }).catch(err => {
      app.appCallBack = (e) => {
        // that.getData()
        that.geStatus()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this;
    const refresh = wx.getStorageSync('refresh');
    if (refresh) {
      wx.setStorageSync('refresh', false)
      that.getData()
    } else {
      let memberData = wx.getStorageSync('memberData')
      console.log(memberData,123132)
      that.setData({
        memberData: memberData,
        isnoshowCard: memberData ? true : false
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.getData();
    that.geStatus()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})